$(function(){
        // 业务1：获取页面数据a
       function articleList(){
        $.ajax({
            type:'get',
            url:'http://localhost:8080/api/v1'+'/admin/article/query',
            data:{},
            headers:{
                Authorization: localStorage.getItem('dsj_token')
            },
            success:(res)=>{
                // console.log('文章列表数据获取成功',res);
                console.log(res.data.data); 
                const html = template('sunList',res)
                // console.log('w文章列表',html);
                $('tbody').html(html)
            }
        })
       }
        //调用页面数据函数
       articleList()
        // 业务2：删除页面数据
        $('tbody').on('click','.delete',function(){
            if(window.confirm('请问是否真的需要删除？')){
                // 拿到自己的id
                let listId = $(this).data('id')
                console.log(listId);
                // 发起ajax请求删除
                $.ajax({
                    type:'post',
                    url:'http://localhost:8080/api/v1'+'/admin/article/delete',
                    headers:{
                        Authorization: localStorage.getItem('dsj_token')
                    },
                    data:{id:listId},
                    success:(res)=>{
                        console.log(res);
                        alert(res.msg)
                        articleList()
                    }
                })
            }
        })
        // 获取文章分类数据
        function typeList(){
            $.ajax({
                type:'get',
                url:'http://localhost:8080/api/v1'+'/index/category',
                headers:{
                    Authorization: localStorage.getItem('dsj_token')
                },
                success:(res)=>{
                    console.log('筛选分类',res);
                    const typeslist_moban = template('typeLists',res)
                    $('#selCategory').html(typeslist_moban)
                    // console.log(typeslist);
                }
            })
        }
        // 调用分类数据函数
        typeList()
        // 业务3：筛选功能
        $('#btnSearch').on('click',function(){
            // 拿到表单的value
            let aname = $('#aname').val().trim() //文章标题搜索表单
            let selCategory = $('#selCategory').val() //分类表单
            let selStatus = $('#selStatus').val() //状态表单
            // console.log(aname,selCategory,selStatus);
            $.ajax({
                type:'get',
                url:'http://localhost:8080/api/v1'+'/admin/article/query',
                data:{
                    key:aname,
                    type:selCategory,
                    state:selStatus
                },
                headers:{
                    Authorization: localStorage.getItem('dsj_token')
                },
                success:(res)=>{
                    // console.log('文章列表数据获取成功',res);
                    // console.log(res.data.data); 
                    const html = template('sunList',res)
                    // console.log('w文章列表',html);
                    $('tbody').html(html)
                }
            })
        })
        // 业务4：根据id获取文章信息
        $('tbody').on('click','.btn-compile',function(){
            // 获取id加到url中，传递给下一个页面
             const id = $(this).data('id')
            location.href='article_edit.html'+'?'+id
        })
})